<!DOCTYPE html>
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="UTF-8">
  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../../web-component-tester/browser.js"></script>
  <link rel="import" href="../../polymer.html">
  <link id="elements" rel="import" href="sub/resolveurl-elements.html">
</head>
<body>
  <x-resolve></x-resolve>

  <dom-module id="x-resolve">
  </dom-module>

  <script>
    addEventListener('HTMLImportsLoaded', function() {
      Polymer({is: 'x-resolve'});
    });
  </script>


  <script>
    suite('ResolveUrl', function() {

      test('Urls in styles and attributes', function() {
        var el = document.createElement('p-r');
        var rx = /sub\/foo\.z/;
        assert.match(el._styles[0].textContent, rx, 'url not relative to main document');
        assert.match(el.$.div.getAttribute('style'), rx, 'style url not relative to main document');
        assert.match(el.$.img.src, rx, 'src url not relative to main document');
        assert.match(el.$.a.href, rx, 'href url not relative to main document');
        assert.match(el.$.zonk.getAttribute('url'), rx, 'url url not relative to main document');
        assert.notMatch(el.$.rel.href, rx, 'relative href url not relative to main document');
        assert.match(el.$.rel.href, /\?123$/, 'relative href does not preserve query string');
        assert.equal(el.$.action.getAttribute('action'), 'foo.z', 'action attribute relativized for incorrect element type');
        assert.match(el.$.formAction.action, rx, 'action attribute relativized for incorrect element type');
        assert.equal(el.$.hash.getAttribute('href'), '#foo.z', 'hash-only url should not be resolved');
      });

      test('resolveUrl api', function() {
        var el = document.createElement('p-r');
        var expected = document.baseURI.replace(/[?#].*$/, '');
        var actual = el.resolveUrl('../resolveurl.html');
        assert.equal(actual, expected);
      });

      test('resolveUrl api, when defined in main doc', function() {
        var el = document.querySelector('x-resolve');
        var expected = document.baseURI.replace(/[?#].*$/, '');
        expected = expected.split('/');
        expected.pop();
        expected = expected.join('/') + '/foo/bar.png';
        var actual = el.resolveUrl('foo/../foo/bar.png');
        assert.equal(actual, expected);
      });

      test('resolveUrl api with assetpath', function() {
        var el = document.createElement('p-r-ap');
        // Manually calculate expected URL, to avoid dependence on
        // URL object for this test for IE! Otherwise, would do this:
        // var importPath = document.querySelector('#elements').href;
        // var expected = new URL('../../assets/Beaker2.jpg', importPath);
        var expected = document.baseURI.replace(/[?#].*$/, '');
        expected = expected.split('/');
        expected.pop();
        expected.pop();
        expected = expected.join('/');
        expected = expected + '/assets/Beaker2.jpg';
        var actual = el.resolveUrl('Beaker2.jpg');
        assert.equal(actual, expected);
      });
    });
  </script>
</body>
</html>
